#include "BlockQueue.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"
void *producer(void *arg)
{

    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);
    std::cout << "Producer is producing items..." << std::endl;
    // 生产逻辑省略
    while (true)
    {
      //  sleep(1); // 模拟生产耗时
        std::cout << "Produce 任务:" << std::endl;

        bq->Equeue(DownLoad);
    }
    return nullptr;
}
void *consumer(void *arg)
{

    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);
    std::cout << "Consumer is consuming items..." << std::endl;
    // 消费逻辑省略
    while (true)
    {
         sleep(2); // 模拟消费耗时
        task_t data = bq->Pop();
        data();
        // std::cout << "Consume 任务:" << data.GetX() << "+" << data.GetY() << "=" << data.GetResult() << std::endl;
    }
    return nullptr;
}

int main()
{
    BlockQueue<task_t> *bq = new BlockQueue<task_t>(); // 申请阻塞队列

    // 构建生产消费
    pthread_t p1[3], c1[2];
    pthread_create(p1, nullptr, producer, (void *)bq);
    pthread_create(p1 + 1, nullptr, producer, (void *)bq);
    pthread_create(p1 + 2, nullptr, producer, (void *)bq);
    pthread_create(c1, nullptr, consumer, (void *)bq);
    pthread_create(c1 + 1, nullptr, consumer, (void *)bq);
    pthread_join(p1[0], nullptr);
    pthread_join(p1[1], nullptr);
    pthread_join(p1[2], nullptr);

    pthread_join(c1[0], nullptr);
    pthread_join(c1[1], nullptr);
    return 0;
}